package com.grailslm

import org.apache.ibatis.io.Resources
import org.apache.ibatis.session.SqlSessionFactory
import org.apache.ibatis.session.SqlSessionFactoryBuilder

/**
 * Example Groovy class.
 */
class Example {

    static SqlSessionFactory sessionFactory = null

    static {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); //得到配置文件的输入流mybatis-config.xml
        sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //创建SessionFactory
    }

    /**
     * 增
     */
    static def save(def params) {
        def session = sessionFactory.openSession()
        try {
            def mapper = session.getMapper(UserMapper)
            def user = new User(params)
            mapper.save(user) //mapper.save(params)
            println user.getId()
            session.commit()
        } finally {
            session.close();
        }
    }

    /**
     * 读
     */
    static def show() {
        def session = sessionFactory.openSession()
        try {
            def mapper = session.getMapper(UserMapper)
            println mapper.get(2)
            session.commit()
        } finally {
            session.close();
        }
    }

    /**
     * 改
     */
    static def update() {
        def session = sessionFactory.openSession()
        try {
            def mapper = session.getMapper(UserMapper)
            User user = mapper.get(2)
            user.realname = "匿名君"
            mapper.update(user)
            session.commit()
        } finally {
            session.close();
        }
    }

    /**
     * 删
     */
    static def delete() {
        def session = sessionFactory.openSession()
        try {
            def mapper = session.getMapper(UserMapper)
            mapper.delete(2)
            session.commit()
        } finally {
            session.close();
        }
    }

     /**
     * 列表
     */
    static def list() {
        def session = sessionFactory.openSession()
        try {
            def mapper = session.getMapper(UserMapper)
            println mapper.list()
            session.commit()
        } finally {
            session.close();
        }
    }

    static def demo() {
        def session = sessionFactory.openSession()
        try {
            def mapper = session.getMapper(UserMapper)
            println mapper.demo()
            session.commit()
        } finally {
            session.close();
        }
    }


    static main(args) {
        try {
//            this.save([username:"superman", password:"123456", realname:"深思君", firstName:"思君", lastName:"深"])
//            this.save([username:"admin", password:"123456", realname:"管理员", firstName:"常龙", lastName:"吕"])
//            this.save([username:"user", password:"123456", realname:"普通用户", firstName:"名", lastName:"姓"])

//            this.show()
//            this.update()
//            this.show()

//            this.delete()
//            this.list()

            this.demo() //演示封装结果时使用的“自动映射”约定
        } catch (Exception e) {
            e.printStackTrace()
        } finally {

        }
    }

}
